/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.flf.audioteca.viewhelper.cadastros;

import br.edu.flf.audioteca.businessdelegate.IArtistaBusinessDelegate;
import br.edu.flf.audioteca.businessdelegate.impl.ArtistaBusinessDelegate;
import br.edu.flf.audioteca.entidades.Album;
import br.edu.flf.audioteca.entidades.Artista;
import br.edu.flf.audioteca.enums.CategoriaDoAlbum;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

/**
 * 
 * @author Nathan Azevedo
 */
public class AlbumViewHelper {

	private IArtistaBusinessDelegate artistaBusinessDelegate = null;
	private static AlbumViewHelper albumViewHelper;
	private Album album = new Album();
	private Artista autor = new Artista();
	private Integer numeroDeMusicas;
	private List<Artista> artistasNoBanco = null;
	private SelectItemGroup itemGroup = new SelectItemGroup();
	public static final String ROTULO_PARA_GRUPO_DE_ARTISTAS = "Nome do Artista - Genero - Local de origem";

	public List<SelectItem> getListaDeCategoriasFormatadas() {
		CategoriaDoAlbum[] categoriasDeAlbums = CategoriaDoAlbum.values();
		List<SelectItem> list = new ArrayList<SelectItem>();

		for (CategoriaDoAlbum cda : categoriasDeAlbums) {
			list.add(new SelectItem(cda, cda.getDescricao()));
		}

		return list;
	}

	public Album getAlbum() {
		return album;
	}

	public void setAlbum(Album album) {
		this.album = album;
	}

	public Integer getAutor() {
		return autor.getCodigoArtista();
	}

	public void setAutor(Integer autorid) {
		this.autor.setCodigoArtista(autorid);
		album.setArtistaDono(autor);
	}

	public Integer getNumeroDeMusicas() {
		return numeroDeMusicas;
	}

	public void setNumeroDeMusicas(Integer numeroDeMusicas) {
		this.numeroDeMusicas = numeroDeMusicas;
	}

	public Collection<SelectItem> getObterListaDeArtistas() {

		artistaBusinessDelegate = ArtistaBusinessDelegate.getInstance();

		artistasNoBanco = artistaBusinessDelegate.obterTodosOsArtistas();

		if (artistasNoBanco == null) {
			artistasNoBanco = new LinkedList<Artista>();
		}

		return obterListaDeSelectItemFormatadaParaArtistas();
	}

	protected Collection<SelectItem> obterListaDeSelectItemFormatadaParaArtistas() {

		Collection<SelectItem> selectItems = new ArrayList<SelectItem>();
		for (Artista artistaAtual : artistasNoBanco) {
			Integer id = artistaAtual.getCodigoArtista();
			String infoArtista = artistaAtual.getNomeDoArtista() + " - "
					+ artistaAtual.getGeneroDoArtista() + " - "
					+ artistaAtual.getLocalDeOrigem();
			selectItems.add(new SelectItem(id, infoArtista));
		}
		return selectItems;

	}

	protected List<SelectItem> formatarParaGrupoDeSelectItemComDescricao(
			List<SelectItem> selectItems, String descricaoDoGrupo) {

		SelectItem[] itensArray = new SelectItem[selectItems.size()];
		for (int i = 0; i < selectItems.size(); i++) {
			itensArray[i] = selectItems.get(i);
		}
		itemGroup.setSelectItems(itensArray);
		itemGroup.setLabel(descricaoDoGrupo);

		selectItems.clear();

		selectItems.add(itemGroup);
		return selectItems;

	}

	public static AlbumViewHelper getInstance() {
		if (albumViewHelper == null) {
			albumViewHelper = new AlbumViewHelper();
		}
		return albumViewHelper;
	}
}
